package com.xlh.controller.problem;

import com.xlh.common.Result;
import com.xlh.dto.PageDTO;
import com.xlh.dto.problem.ProblemDTO;
import com.xlh.param.problem.ForbiddenParam;
import com.xlh.param.problem.ProblemParam;
import com.xlh.param.problem.ProblemPostParam;
import com.xlh.param.problem.ReplyPostParam;
import com.xlh.pojo.problem.PcConfig;
import com.xlh.service.problem.ProblemCommunicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author sy
 */
@Api(tags = "ProblemCommunicationController", description = "问题交流管理")
@RestController
@RequestMapping("/problemCommunication")
public class ProblemCommunicationController {

    @Autowired
    private ProblemCommunicationService problemCommunicationService;

    @ApiOperation("分页查询问题列表")
    @GetMapping("/listProblemPages")
    @PreAuthorize("hasAnyRole('TEACHER','STUDENT','MARKET_TEACHER','UNIVERSITY_TEACHER')")
    public Result listProblemPages(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @Valid ProblemParam problemParam) {
        return Result.success(problemCommunicationService.listProblemPages(pageNum,pageSize,problemParam));
    }

    @ApiOperation("分页查询我的问题列表")
    @GetMapping("/listMyProblemPages")
    @PreAuthorize("hasAnyRole('TEACHER','STUDENT','MARKET_TEACHER','UNIVERSITY_TEACHER')")
    public Result listMyProblemPages(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @Valid ProblemParam problemParam) {
        return Result.success(problemCommunicationService.listMyProblemPages(pageNum,pageSize,problemParam));
    }

    @ApiOperation("查询问题列表问题详情")
    @GetMapping("/problemDetail/{id}")
    @PreAuthorize("hasAnyRole('TEACHER','STUDENT','MARKET_TEACHER','UNIVERSITY_TEACHER')")
    public Result problemDetail(@PathVariable @ApiParam(value = "问题主键",required = true) Long id) {
        return Result.success(problemCommunicationService.problemDetail(id));
    }

    @ApiOperation("查询我的回答列表问题详情")
    @GetMapping("/myAnswerProblemDetail/{id}")
    @PreAuthorize("hasAnyRole('TEACHER','STUDENT','MARKET_TEACHER','UNIVERSITY_TEACHER')")
    public Result myAnswerProblemDetail(@PathVariable @ApiParam(value = "问题主键",required = true) Long id) {
        return Result.success(problemCommunicationService.myAnswerProblemDetail(id));
    }

    @ApiOperation("分页查询我的回答列表")
    @GetMapping("/listMyProblemReplyPages")
    @PreAuthorize("hasAnyRole('TEACHER','STUDENT','MARKET_TEACHER','UNIVERSITY_TEACHER')")
    public Result<PageDTO<ProblemDTO>> listMyProblemReplyPages(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @Valid ProblemParam problemParam) {
        return Result.success(problemCommunicationService.listMyProblemReplyPages(pageNum,pageSize,problemParam));
    }

    @ApiOperation("禁言管理")
    @GetMapping("/forbiddenWordsListPage")
    @PreAuthorize("hasAnyRole('TEACHER','MARKET_TEACHER','UNIVERSITY_TEACHER')")
    public Result forbiddenWordsListPage(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @Valid ProblemParam problemParam) {
        return Result.success(problemCommunicationService.forbiddenWordsListPage(pageNum,pageSize,problemParam));
    }

    @ApiOperation("禁言or解除禁言")
    @PostMapping("/forbiddenWords")
    @PreAuthorize("hasAnyRole('TEACHER','MARKET_TEACHER','UNIVERSITY_TEACHER')")
    public Result forbiddenWords(@RequestBody @Valid ForbiddenParam param) {
        problemCommunicationService.forbiddenWords(param);
        return Result.success();
    }

    @ApiOperation("我要提问")
    @PostMapping("/askQuestions")
    @PreAuthorize("hasAnyRole('TEACHER','STUDENT','MARKET_TEACHER','UNIVERSITY_TEACHER')")
    public Result askQuestions(@RequestBody @Valid ProblemPostParam problemPostParam) {
        problemCommunicationService.askQuestions(problemPostParam);
        return Result.success();
    }

    @ApiOperation("我要回答")
    @PostMapping("/toAnswer")
    @PreAuthorize("hasAnyRole('TEACHER','STUDENT','MARKET_TEACHER','UNIVERSITY_TEACHER')")
    public Result toAnswer(@RequestBody @Valid ReplyPostParam replyPostParam) {
        problemCommunicationService.toAnswer(replyPostParam);
        return Result.success();
    }

    @ApiOperation("批量删除我的回答")
    @PostMapping("/batchDeletionAnswer")
    @PreAuthorize("hasAnyRole('TEACHER','STUDENT','MARKET_TEACHER','UNIVERSITY_TEACHER')")
    public Result batchDeletionAnswer(@RequestBody List<Integer> ids) {
        problemCommunicationService.batchDeletionAnswer(ids);
        return Result.success();
    }

    @ApiOperation("批量删除我的提问")
    @PostMapping("/batchDeletionQuestion")
    @PreAuthorize("hasAnyRole('TEACHER','STUDENT','MARKET_TEACHER','UNIVERSITY_TEACHER')")
    public Result batchDeletionQuestion(@RequestBody List<Integer> ids) {
        problemCommunicationService.batchDeletionQuestion(ids);
        return Result.success();
    }

    @ApiOperation("查询问题交流开关")
    @GetMapping("/getProblemSwitch/{id}")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result getProblemSwitch(@PathVariable @ApiParam(value = "课程id",required = true) Long id) {
        return Result.success(problemCommunicationService.getProblemSwitch(id));
    }

    @ApiOperation("开启学生端问题交流or关闭学生端问题交流")
    @PostMapping("/controlSwitch")
    @PreAuthorize("hasAnyRole('TEACHER','MARKET_TEACHER','UNIVERSITY_TEACHER')")
    public Result controlSwitch(@RequestBody @Valid PcConfig pcConfig) {
        problemCommunicationService.controlSwitch(pcConfig);
        return Result.success();
    }
}
